package com.reminder;
/*code used and modified from http://bradabrams.com/2011/05/google-io-session-overview-android-app-engine-a-developers-dream-combination*/

import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.reminder.shared.ReminderRequestFactory;
import com.reminder.shared.TaskProxy;
import com.reminder.shared.ReminderRequest;
import com.google.web.bindery.requestfactory.shared.Receiver;
import com.google.web.bindery.requestfactory.shared.ServerFailure;

public class ViewTaskActivity extends Activity {
    private java.text.DateFormat dateFormat;
    private TaskProxy task;
    private TaskProxy taskGeo;
    private int geoTask;
    private int count; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getIntent().getExtras() != null ){
        	
        	geoTask = getIntent().getIntExtra("geoTask", -1);
        	count = getIntent().getIntExtra("count", -1);
        }
        
        
        dateFormat = DateFormat.getDateFormat(this);

        setContentView(R.layout.viewtask);
        if(geoTask == 1) {
        	taskGeo = GpsService.listOfTasks.get(count);
        	TextView titleText = (TextView) findViewById(R.id.titleText);
        	titleText.setText(GpsService.listOfTasks.get(count).getName());
        	TextView detailsText = (TextView) findViewById(R.id.detailsText);
            detailsText.setText(GpsService.listOfTasks.get(count).getNote());
            TextView dueDateText = (TextView) findViewById(R.id.dateText);
            dueDateText.setText(dateFormat.format(GpsService.listOfTasks.get(count).getDueDate()));
        }

        // get the task to display.
        Intent intent = getIntent();
        int position = intent.getIntExtra("position", -1);
        

        if (position != -1) {
            TaskApplication taskApplication = (TaskApplication) getApplication();
            TaskAdapter adapter = taskApplication.getAdapter(this);

           // TaskProxy task = adapter.get(position);
            task = adapter.get(position);
            if (task != null) {
                String name = task.getName();
                if (name != null) {
                   TextView titleText = (TextView) findViewById(R.id.titleText);
                    titleText.setText(name);
                }

                String notes = task.getNote();
                if (notes != null) {
                    TextView detailsText = (TextView) findViewById(R.id.detailsText);
                    detailsText.setText(notes);
                }

                Date dueDate = task.getDueDate();
                if (dueDate != null) {
                    TextView dueDateText = (TextView) findViewById(R.id.dateText);
                    dueDateText.setText(dateFormat.format(dueDate));
                }
            }
        }
    }
    
    public void onDeleteClick(View view) {
    	new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(Void... arg0) {
    	ReminderRequestFactory factory = (ReminderRequestFactory) Util
                .getRequestFactory(ViewTaskActivity.this,
                        ReminderRequestFactory.class);
        ReminderRequest request = factory.taskRequest();
        request.deleteTask(task).fire();
        return null;
            }

        }.execute();
   	 Intent t = new Intent(this, ReminderActivity.class);
   	 setResult(Activity.RESULT_OK, t);
   	 finish();
   }
}
